using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
 

public partial class Recruiters_responseletters : System.Web.UI.Page
{
    string connectionString = "";
    int itemPerPage = 15;
    int pagePerSeggment = 5;

    void RegisterJavaScript()
    {
        Common common = new Common();
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "openWindow" + this.GridView1.ID, common.CreateScriptForPrint(this.GridView1.ID));
        this.btnPrint.Attributes.Add("onclick", "openWindow" + this.GridView1.ID + "('" + GridView1.ID.ToString() + "',400,700);return false;");

    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["rcuserid"] == null)
            Response.Redirect("login.aspx");
        connectionString =
         WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
        RegisterJavaScript();

        if (!IsPostBack)
        {            
             ShowResponse();
        }

         

    }


    void ShowResponse()
    {
        try
        {
            SqlServerProvider cls = new SqlServerProvider(connectionString);
            cls.CommandText = "SPViewResponseLetter";
            cls.ParameterCollection = new string[1] { "@RecruiterID" };
            cls.ValueCollection = new string[1] { 
            Convert.ToString(Session.Contents["rcuserid"]) };
            cls.CommandType = CommandType.StoredProcedure;
            DataTable dataTable = cls.GetDataTable();
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataTableToGridView(
            dataTable, this.GridView1);

            if (GridView1.Rows.Count >= 1)
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string deleteQuery = "";
        string selectItem = GetSelectItem();         
        if (selectItem != "")
        {
            selectItem = selectItem.Substring(
                0, selectItem.Length - 1);
         
             
            RecruitVietnam recruitVietnam
                        = new RecruitVietnam();
            recruitVietnam.ConnectionString = connectionString;
            deleteQuery =
            "delete from tblResponseLetters " +
            " where ResponseID in (" + selectItem +
            ") and RecruiterID ="
            + Session["rcuserid"].ToString();
            if (recruitVietnam.ExecuteSQL(deleteQuery))
                ShowResponse();
             
        }
    }
    string GetSelectItem()
    {
        string selectItem = "";
        foreach (GridViewRow gridViewRow
        in GridView1.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkResponseID");
            if (checkBox.Checked)
                selectItem +=
                 gridViewRow.Cells[1].Text + ",";

        }

        return selectItem;
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("responseletter.aspx");
    }
}
